import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:weui/icon/index.dart';
import 'package:yueyin/global/global_theme.dart';
import 'package:yueyin/service/theme_service.dart';
import 'package:yueyin/utils/yue_bar.dart';

class ThemeSettingView extends StatefulWidget {
  const ThemeSettingView({Key? key}) : super(key: key);

  @override
  _ThemeSettingViewState createState() => _ThemeSettingViewState();
}

class _ThemeSettingViewState extends State<ThemeSettingView> {
  late ThemeService _service;

  late int _color;

  @override
  void initState() {
    super.initState();
    _service = Provider.of<ThemeService>(context,listen: false);
    _color = _service.color;

  }

  @override
  Widget build(BuildContext context) {
    _service.addListener(() {
      setState(() {
        _color = _service.color;
      });
    });
    return Scaffold(
      appBar: bar('设置主题'),
      body: ListView.builder(itemBuilder: itemBuilder),
    );
  }

  Row _row(int index) {
    return _color == index
        ? Row(mainAxisAlignment: MainAxisAlignment.end, children: [
            Icon(WeIcons.hook, color: Colors.white),
            SizedBox(width: 16)
          ])
        : Row();
  }

  Widget itemBuilder(BuildContext context, int index) {
    return Padding(
      padding: EdgeInsets.all(10),
      child: TextButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(themeColors[index]),
        ),
        onPressed: () {
          _service.change(index);
        },
        child: _row(index),
      ),
    );
  }
}
